﻿using PPSMS_Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPSMS_DAL;

public class WeatherInfo_DAL : ICRUD_DAL<WeatherInfo, long>
{
    /// <summary>
    /// 删除指定ID的记录
    /// </summary>
    /// <param name="ids">气象信息表id数组</param>
    public void DeleteByIds(params long[] ids)
    {
        string sql = $"delete from WEATHER_INFO where Id in ({string.Join(",", ids)})";
        DBHelper.ExecuteNonQuery(sql);
    }

    /// <summary>
    ///  获取当前最大ID   
    /// </summary>
    /// <returns>ID</returns>
    public long GetCurrentValue()
    {
        string sql = "select rowid from WEATHER_INFO";
        return DBHelper.ExecuteQuery(sql, DBHelper.ExecuteScalarToInt, new object[0]);
    }

    /// <summary>
    /// 新增记录
    /// </summary>
    /// <param name="t">气象信息表对象</param>
    public void Insert(WeatherInfo t)
    {
        string sql = "insert into WEATHER_INFO (TIMESTAMP, IRRADIANCE,TEMPERATURE, HUMIDITY, WINDSPEED) values (?,?,?,?,?)";
        DBHelper.ExecuteNonQuery(sql, t.Timestamp, t.Irradiance, t.Temperature, t.Humidity, t.Windspeed);
    }

    /// <summary>
    /// 将一个 DataRow 对象转换为对应的 WeatherInfo 对象
    /// </summary>
    /// <param name="dr"></param>
    /// <returns>气象信息表对象</returns>
    public WeatherInfo ResultHandler(DataRow dr)
    {
        return new WeatherInfo
        {
            Timestamp = dr.Field<string>("TIMESTAMP"),
            Irradiance = dr.Field<double>("IRRADIANCE"),
            Humidity = dr.Field<double>("HUMIDITY"),
            Windspeed = dr.Field<double>("WINDSPEED")
        };
    }

    /// <summary>
    /// 将一个 DataTable 对象转换为对应的 List<WeatherInfo> 对象
    /// </summary>
    /// <param name="dr"></param>
    /// <returns>一个包含所有气象信息表信息的List</returns>
    public List<WeatherInfo> ResultHandlerList(DataTable dr)
    {
        List<WeatherInfo> list = new List<WeatherInfo>();
        foreach (DataRow dr1 in dr.Rows)
        {
            list.Add(ResultHandler(dr1));
        }
        return list;
    }

    /// <summary>
    /// 获取所有气象信息表的记录
    /// </summary>
    /// <returns>一个包含所有气象信息表信息的List</returns>
    public List<WeatherInfo> SelectAll()
    {
        string sql = "select * from WEATHER_INFO";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]);
    }

    /// <summary>
    ///  获取指定日期范围内的气象信息表的记录
    /// </summary>
    /// <param name="Timestamp"></param>
    /// <returns></returns>
    public List<WeatherInfo> SelectAllByDate(string Timestamp)
    {
        string sql1 = $"select * from WEATHER_INFO where Timestamp like '____-12-31 23%' ORDER BY Timestamp ASC";
        string sql2 = $"select * from WEATHER_INFO where Timestamp like '{Timestamp}%' ORDER BY Timestamp ASC";
        if (Timestamp != "ALL")
        {
            sql1 = sql2;
        }
        return DBHelper.ExecuteQuery(sql1, ResultHandler, new object[0]);
    }

    /// <summary>
    /// 获取指定ID的气象信息表记录
    /// </summary>
    /// <param name="id"></param>
    /// <returns>气象信息表对象</returns>
    public WeatherInfo? SelectById(long id)
    {
        return SelectAll().FirstOrDefault(x => x.Id == id);
    }

    /// <summary>
    /// 根据条件修改气象信息表的记录
    /// </summary>
    /// <param name="t">新的气象信息表对象</param>
    /// <param name="id">旧的ID</param>
    /// <exception cref="ArgumentException"></exception>
    public void UpdateById(WeatherInfo t, long id)
    {
        string sql = "updata WEATHER_INFO set ";
        var list = new List<object>();
        if (t.Timestamp != null)
        {
            sql += "TIMESTAMP = ? ,";
            list.Add(t.Timestamp);
        }
        if (t.Irradiance != null)
        {
            sql += "IRRADIANCE = ? ,";
            list.Add(t.Irradiance);
        }
        if (t.Humidity != null)
        {
            sql += "HUMIDITY = ? ,";
            list.Add(t.Humidity);
        }
        if (t.Windspeed != null)
        {
            sql += "WINDSPEED = ? ,";
            list.Add(t.Windspeed);
        }
        sql = sql.TrimEnd(',');
        if (list.Count == 0) throw new ArgumentException("没有任何属性需要更新");
        sql += " where ID = ?";
        list.Add(id);
        DBHelper.ExecuteNonQuery(sql, list.ToArray());
    }
}
